package com.aaa.dao.impl;

import com.aaa.dao.RoleDao;
import com.aaa.entity.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class RoleDaoImpl implements RoleDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    private RowMapper<Role> roleRowMapper = new RowMapper<Role>() {
        public Role mapRow(ResultSet resultSet, int i) throws SQLException {
            Role role = new Role();
            role.setId(resultSet.getInt("id"));
            role.setName(resultSet.getString("name"));
            return role;
        }
    };


    /**
     * 保存角色能操作的模块
     * @param rid
     * @param mid
     * @return
     */
    public Integer saveRoleMenu(Integer rid,Integer mid){
        String sql = "insert into role_menu(r_id,m_id) values(?,?)";
        int count = this.jdbcTemplate.update(sql,rid,mid);
        return  count;
    }

    /**
     * 删除角色能操作的模块
     * @param rid
     * @return
     */
    public Integer deleteRoleMenu(Integer rid){
        String sql = "delete from role_menu where r_id=?";
        int count = this.jdbcTemplate.update(sql,rid);
        return  count;
    }


    public List<Role> listAll(String keyword) {
        String sql = "select id,name from role where name like ? order by id desc";
        List<Role> query = this.jdbcTemplate.query(sql, roleRowMapper,"%"+keyword+"%");
        return query;
    }
    /**
     * 查询总条数
     * @return
     */
    public Integer count(String keyword){
        String sql = "select count(id) as c from role where name like ? ";
        Integer result  = this.jdbcTemplate.queryForObject(sql, new RowMapper<Integer>() {
            public Integer mapRow(ResultSet resultSet, int i) throws SQLException {
                return resultSet.getInt("c");
            }
        },"%"+keyword+"%");
        return result;
    }



    /**
     * 保存角色 并获取自动生成的主键
     * @param role
     * @return
     */
    public Integer save(final Role role){
        final String sql="insert into role (name) values(?)";
        KeyHolder keyHolder = new GeneratedKeyHolder();
        jdbcTemplate.update(new PreparedStatementCreator() {

            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException{
                PreparedStatement ps = connection.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
                ps.setString(1, role.getName());
                return ps;
            }
        }, keyHolder);
        return keyHolder.getKey().intValue();
    }

    public Integer update(Role role) {
        String sql = "update role set name=? where id=? ";
        int update = this.jdbcTemplate.update(sql, role.getName(), role.getId());
        return update;
    }

    public Integer delete(Long id) {
        return null;
    }

    public Integer delete(Integer id) {
        String sql = "delete from role where id=?";
        int update = this.jdbcTemplate.update(sql, id);
        return update;
    }

    public List<Integer> checkIdRole(Integer id) {
        String sql="select r_id from user_role where u_id=?";
        List<Integer> rid = jdbcTemplate.query(sql, new RowMapper<Integer>() {
            public Integer mapRow(ResultSet resultSet, int i) throws SQLException {
                return resultSet.getInt("r_id");
            }
        },id);
        return rid;
    }
}
